<template>
  <el-card>
    <template #header>
      <h3>店铺管理</h3>
    </template>

    <div>
      <!-- 表单 -->
      <el-form
        :model="shopForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="shopForm.name"></el-input>
        </el-form-item>
        <el-form-item label="店铺公告" prop="bulletin">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="shopForm.bulletin"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="商铺头像">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/shop/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="商铺图片">
          <el-upload
            action="http://127.0.0.1:5000/shop/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="picSuccess"
            :file-list="fileList"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <!-- 图片展开放大功能 -->
          <el-dialog :visible.sync="picShow">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="配送费" prop="deliveryPrice">
          <el-input v-model="shopForm.deliveryPrice"></el-input>
        </el-form-item>
        <el-form-item label="配送时间" prop="deliveryTime">
          <el-input v-model="shopForm.deliveryTime"></el-input>
        </el-form-item>
        <el-form-item label="配送描述" prop="description">
          <el-input v-model="shopForm.description"></el-input>
        </el-form-item>
        <el-form-item label="店铺评分" prop="score">
          <el-input v-model="shopForm.score"></el-input>
        </el-form-item>
        <el-form-item label="销量" prop="sellCount">
          <el-input v-model="shopForm.sellCount"></el-input>
        </el-form-item>
        <el-form-item label="活动" prop="supports">
          <el-checkbox-group v-model="shopForm.supports">
            <el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
            <el-checkbox label="在线支付满28减5"></el-checkbox>
            <el-checkbox label="单人精彩套餐"></el-checkbox>
            <el-checkbox label="单人特色套餐"></el-checkbox>
            <el-checkbox label="特价饮品8折抢购"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="营业时间" prop="date">
          <el-time-picker
            is-range
            v-model="shopForm.date"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
          >
          </el-time-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { getShopDetail, editShopInfo } from '@/api/shop.js'
import _ from 'lodash'
export default {
  data() {
    return {
      shopForm: {
        name: '',
        bulletin: '',
        deliveryPrice: '',
        deliveryTime: '',
        description: '',
        sellCount: '',
        score: '',
        supports: [],
        date: []
      },
      rules: {
        name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        bulletin: [
          { required: true, message: '请输入商品公告', trigger: 'blur' }
        ],
        deliveryPrice: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        score: [{ required: true, message: '不能为空', trigger: 'blur' }],
        deliveryTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        description: [{ required: true, message: '不能为空', trigger: 'blur' }],
        sellCount: [{ required: true, message: '不能为空', trigger: 'blur' }],
        supports: [
          { required: true, message: '请选择活动', trigger: 'change' }
        ],
        date: [{ required: true, message: '请选择营业时间', trigger: 'change' }]
      },

      // 头像上传的信息
      avatatBaseUrl: 'http://127.0.0.1:5000/upload/shop/',
      imageUrl: '',

      // 店铺图片的信息
      dialogImageUrl: '',
      picShow: false,
      fileList: []
    }
  },
  created() {
    this.getShop()
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 这是表单验证通过
          const cloneForm = _.cloneDeep(this.shopForm)
          cloneForm.supports = JSON.stringify(cloneForm.supports)
          const arr = []
          this.fileList.forEach(item => {
            arr.push(item.url)
          })
          cloneForm.pics = JSON.stringify(arr)
          cloneForm.date = JSON.stringify(cloneForm.date)
          const data = await editShopInfo(cloneForm)
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            location.reload()
          }
        } else {
          this.$message.error('请按提示填写信息!')
          return false
        }
      })
    },
    picSuccess(res, file) {
      // 在this.fileList 中添加一个对象
      this.fileList.push({
        name: res.imgUrl,
        url: this.avatatBaseUrl + res.imgUrl
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.shopForm.avatar = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
      const isJPG = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter(item => item.url !== file.url)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.picShow = true
    },
    async getShop() {
      const { data } = await getShopDetail()
      this.imageUrl = this.avatatBaseUrl + data.avatar
      this.fileList = []
      //遍历pic,整理成对象的格式.push给数组
      data.pics.forEach(item => {
        this.fileList.push({
          name: item,
          url: item
        })
      })
      this.shopForm = data
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 250px;
}
.el-select {
  width: 250px;
}
/deep/.el-form-item__label {
  width: 80px !important;
}
/deep/ .el-textarea {
  width: 400px;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
